using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class UserControls_Admin_EditImageAdmin : System.Web.UI.UserControl
{
    string imageId, galleryId;
    protected void Page_Load(object sender, EventArgs e)
    {
        imageId = Request.QueryString["ImageID"];
        galleryId = Request.QueryString["GalleryID"];
        // Fill the controls with data only on the initial page load
        if (!IsPostBack)
        {
            // Fill controls with data
            PopulateControls();
        }
    }

    // Populate the controls
    private void PopulateControls()
    {
        ImageDetails imd = PhotoGallery.GetImageDetails(imageId);
        if (Int32.Parse(galleryId) == 1)
        {
            //image1.ImageUrl = Request.ApplicationPath + "/PhotoGallery/Sklep/Thumbs/" + imd.Image1FileName;
            image2.ImageUrl = Request.ApplicationPath + "/PhotoGallery/Sklep/Regular/" + imd.Image2FileName;
        }
        if (Int32.Parse(galleryId) == 2)
        {
           // image1.ImageUrl = Request.ApplicationPath + "/PhotoGallery/Klienci/Thumbs/" + imd.Image1FileName;
            image2.ImageUrl = Request.ApplicationPath + "/PhotoGallery/Klienci/Regular/" + imd.Image2FileName;
        }
    }

    protected void upload2Button_Click(object sender, EventArgs e)
    {

        if (image2FileUpload.HasFile == true)
        {
            //if (image2FileUpload.PostedFile.ContentLength >= 1024000)
             //   return;

            try
            {

                string image1 = imageId + ".jpg";
                string image2 = imageId + ".jpg";

                // update database with new product details
                ImageDetails imd = PhotoGallery.GetImageDetails(imageId);
                PhotoGallery.UpdateImage(imageId, imd.Name,
                imd.Description, image1, image2);
                ImageHelperForGallery.StorePicture(image2FileUpload.PostedFile.InputStream, imageId, galleryId);
                // reload the page
                Response.Redirect(Request.ApplicationPath + "/Admin/EditImageAdmin.aspx?GalleryID=" +
                galleryId + "&ImageID=" + imageId);

                statusLabel.Text = "Aktualizacja obrazka udana!";

            }
            catch
            {
                statusLabel.Text = "Auktalizacja obrazka nieudana!";
            }
        }
        else
        {
            Response.Redirect(Request.ApplicationPath + "/Admin/EditImageAdmin.aspx?GalleryID=" + 
            galleryId + "&ImageID=" + imageId);
        }
    }
    protected void LinkButton1_Click1(object sender, EventArgs e)
    {
        Response.Redirect(Request.ApplicationPath + "/Admin/ImagesAdmin.aspx?GalleryID=" + galleryId);
    }
}
